﻿using System;
using System.Runtime.InteropServices;

namespace RayDen.RayEngine.IntersectionEngines
{
    [StructLayout(LayoutKind.Explicit, Size = 32)]
    public struct LNode
    {
        [FieldOffset(0)]
        public BBox Bounds;


        [FieldOffset(24)]
        public UInt32 UData;

        [FieldOffset(28)]
        public byte BData;



        [FieldOffset(29)]
        public byte b0;
        [FieldOffset(30)]
        public byte b1;
        [FieldOffset(31)]
        public byte b2;


        public UInt32 SkipIndex
        {
            get
            {
                return UData;
            }
            set
            {
                this.UData = value;
            }
        }

        /// <summary>
        /// !000 0000 bit is (0/1) whenever node is leaf or not
        /// </summary>
        public bool IsLeaf
        {
            get
            {
                return ((this.BData & 0x80) >> 7) == 1;
            }

            set
            {
                var val = value ? 1 : 0;
                this.BData |= (byte)(val << 7);
            }
        }


        public UInt32 PrimitiveIndex
        {
            get
            {
                return this.UData;
            }
            set
            {
                this.UData = value;
            }
        }

        /// <summary>
        /// 0000 00!! bits is node axis
        /// </summary>
        public byte Axis
        {
            get
            {
                return (byte)(this.BData & 0x03);
            }

            set
            {
                this.BData |= value;
            }
        }
    }
}